from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework import mixins
from rest_framework.generics import ListCreateAPIView

from apps.meiduo_admin.serializers.user import UserSerializer, UserAddSerializer
from apps.users.models import User
from apps.meiduo_admin.utils import PageNum

#########查询用户#################
class UserListAPIView(ListCreateAPIView):

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            #返回查询数据
            data = User.objects.filter(username__contains=keyword,is_staff=False).order_by("id")
        else:
            # 返回所有数据
            data = User.objects.filter(is_staff=False).order_by("id")

        return data

    def get_serializer_class(self):
        if self.request.method == "GET":
            return UserSerializer
        elif self.request.method == "POST":
            return UserAddSerializer

    # 指定查询集--->普通用户
    queryset = User.objects.filter(is_staff=False).order_by("id")
    # 指定序列化器 UserSerializer 对象转字典
    serializer_class = UserSerializer

    # 分页器
    pagination_class = PageNum